#include "frame.h"
#include "frame_pool.h"

Frame::~Frame()
{
	if (!isFromPool)
	{
		return;
	}
	if (data != nullptr)
	{
		delete[] data;
		data = nullptr;
		used = 0;
	}
}

void Frame::ReturnFrame()
{
	Reset();
	pool.ReturnFrame(this);
}
FrameWrapper::~FrameWrapper() 
{
	ReturnFrame();
}
int FrameWrapper::Assign(char* buf, size_t len)
{
	ASSERT(buf != nullptr && len > 0 && len <= frame->size);
	int ret = memcpy_s(frame->data, frame->size, buf, len);
	if (ret == 0)
	{
		frame->used = len;
		return len;
	}
	// return memcpy_s return code when memcpy_s failed
	return -1;
}
void FrameWrapper::ReturnFrame()
{
	if (!hasReturned)
	{
		frame->ReturnFrame();
		frame = nullptr;
	}
	hasReturned = true;
}